GAME.Name = "Game Select"
GAME.Author = "Devenger"
GAME.Version = "version 1"

local scrSize = tgm.ScreenSize

local sltbx_x = scrSize * 0.07
local sltbx_w = scrSize * 0.86
local sltbx_h = scrSize * 0.14
local sltbx_yStart = scrSize * 0.16
local sltbx_yAdd = scrSize * 0.15
local sltbx_xTxtOffset = scrSize * 0.01
local sltbx_yTxtOffset = scrSize * 0.01
local sltbx_yTxtOffset2 = scrSize * 0.07

surface.CreateFont ("coolvetica", scrSize * 0.1, 300, true, false, "CV50")
surface.CreateFont ("coolvetica", scrSize * 0.08, 300, true, false, "CV40")
surface.CreateFont ("coolvetica", scrSize * 0.065, 300, true, false, "CV30")

GAME.Games = {}
GAME.HighlightedGame = false

function GAME:Initialize ()
	self.Games = {}
	for k,v in pairs (self.parent.Games) do
		if v.Selectable or (tgm.CVAR_ShowUnfinishedGames:GetBool() and k != "base" and k != "game_select") then
			table.insert (self.Games, v)
		end
	end
end

function GAME:Think ()
	local x,y = self:MousePos()
	self.HighlightedGame = false
	for i,game in pairs (self.Games) do
		if (x > sltbx_x)
		and (x < sltbx_x + sltbx_w)
		and (y > sltbx_yStart + ((i-1) * sltbx_yAdd))
		and (y < sltbx_yStart + ((i-1) * sltbx_yAdd) + sltbx_h)
		then
			self.HighlightedGame = i
		end
	end
end

function GAME:BindPressed (bind)
	if bind == "+attack" then
		if self.HighlightedGame then
			print ("Selected "..self.Games[self.HighlightedGame].codename)
			self.parent:ChangeGame (self.Games[self.HighlightedGame].codename)
		end
	end
end

function GAME:Render ()
	surface.SetDrawColor (100, 100, 100, 255)
 	surface.DrawRect (scrSize * 0.05, scrSize * 0.05, scrSize * 0.9, scrSize * 0.9)
	draw.SimpleText("Select a game", "CV50", scrSize * 0.5, scrSize * 0.06, Color(255,255,255,255), TEXT_ALIGN_CENTER)
	for i,game in pairs (self.Games) do
		if self.HighlightedGame == i then
			surface.SetDrawColor (0, 0, 0, 255)
		else
			surface.SetDrawColor (50, 50, 50, 255)
		end
		surface.DrawRect (sltbx_x, sltbx_yStart + ((i-1) * sltbx_yAdd), sltbx_w, sltbx_h)
		draw.SimpleText(game.Name, "CV40", sltbx_x + sltbx_xTxtOffset, sltbx_yStart + sltbx_yTxtOffset + ((i-1) * sltbx_yAdd), Color(255,255,255,255))
		draw.SimpleText(game.Author, "CV30", sltbx_x + sltbx_w - sltbx_xTxtOffset, sltbx_yStart + sltbx_yTxtOffset + ((i-1) * sltbx_yAdd), Color(255,255,255,255), TEXT_ALIGN_RIGHT)
		draw.SimpleText(game.Version, "CV30", sltbx_x + sltbx_w - sltbx_xTxtOffset, sltbx_yStart + sltbx_yTxtOffset + ((i-1) * sltbx_yAdd) + sltbx_h - sltbx_yTxtOffset2, Color(255,255,255,255), TEXT_ALIGN_RIGHT)
	end
end
